﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CapaNegocio;
using Entidades;


using System.Data;
using System.Data.SqlClient;

namespace CapaPresentacion
{
    public partial class CatalogoProductos : System.Web.UI.Page
    {

        //SqlCommand cmdCarrito;
        

        public void cargarDetalle()
        {
            DataTable dtb = new DataTable("Carrito");
            dtb.Columns.Add("Codigo", System.Type.GetType("System.Int32"));            
            dtb.Columns.Add("Descripcion", System.Type.GetType("System.String"));          
            dtb.Columns.Add("Precio", System.Type.GetType("System.Double"));            
            dtb.Columns.Add("Cantidad", System.Type.GetType("System.Int32"));
            dtb.Columns.Add("Total", System.Type.GetType("System.Double"));            
            Session["pedido"] = dtb;

        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack==false)
            {
                cargarDetalle();
            }
                
           
        }


        public void agregarItem(int cod, string des, double precio)
        { 
          
            double total;
            int cantidad=1;
            total=precio*cantidad;
            DataTable carrito = new DataTable();
            carrito = (DataTable)Session["pedido"];
            DataRow fila = carrito.NewRow();
            fila[0] = cod;
            fila[1] = des;
            fila[2] = precio;
            fila[3] = (int)cantidad;
            fila[4] = total;
            carrito.Rows.Add(fila);
            Session["pedido"] = carrito;
        
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {   int cod;
            string  des=null,nom=null;
            double precio=0;
            if (e.CommandName == "Agregar")
            {
                DataList1.SelectedIndex = e.Item.ItemIndex;           
                
                cod = System.Convert.ToInt32(((Label)this.DataList1.SelectedItem.FindControl("IdProductoLabel")).Text);
                nom = ((Label)this.DataList1.SelectedItem.FindControl("NombreLabel")).Text;
                des = ((Label)this.DataList1.SelectedItem.FindControl("DescripcionLabel")).Text;
                precio = System.Convert.ToDouble(((Label)this.DataList1.SelectedItem.FindControl("PrecioLabel")).Text);
                agregarItem(cod, nom+" "+des, precio);
                Session["prueba"] = "Sesion Usuario Prueba";

            }
        }

        protected void btnCarrito_Click(object sender, EventArgs e)
        {
            // Session["prueba"] = "Sesion Usuario Prueba";
            Response.Redirect("/CarritoCompras.aspx");

        }

        protected void btnVerCarrito_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("/CarritoCompras.aspx");
        }

       
       

        

       

       
    }
}